package com.tanhua.admin.controller;

import com.tanhua.admin.service.ManageService;
import com.tanhua.model.db.Movement;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @auther : 白月尘
 * @date : 15:08  2021/10/31
 */
@RestController
@RequestMapping("/manage")
public class ManageController {
    @Autowired
    private ManageService manageService;

    //查询用户详情
    @GetMapping("/users/{userID}")
    public ResponseEntity findByUserID(@PathVariable("userID") long userID) {
        UserInfo userInfo = manageService.findByUserID(userID);
        return ResponseEntity.ok(userInfo);
    }

    //查询用户
    @GetMapping("/users")
    public ResponseEntity findUsers(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = manageService.findUsers(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    //查询视频列表
    @GetMapping("/videos")
    public ResponseEntity findVideos(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize,
                                     long uid) {
        PageResult pageResult = manageService.findVideos(page, pagesize, uid);
        return ResponseEntity.ok(pageResult);
    }

    //查询动态
    @GetMapping("/messages")
    public ResponseEntity findMovements(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize,
                                        Long uid, String state) {
        Integer status = null;
        if (!StringUtils.isEmpty(state) && !"''".equals(state)) {
            status = Integer.valueOf(state);
        }
        PageResult pageResult = manageService.findMovements(page, pagesize, uid, status);
        return ResponseEntity.ok(pageResult);
    }

    //查询动态详情
    @GetMapping("/messages/{id}")
    public ResponseEntity findMovementById(@PathVariable("id") String id) {
        Movement vo = manageService.findMovementById(id);
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/messages/comments")
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String messageID) {
        return ResponseEntity.ok(null);
    }
/*
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String s) {
        String[] split = s.split(",");
        String message = manageService.pass(split);
        return ResponseEntity.ok(message);
    }*/
    /*
     * 动态通过
     * */
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String[] message) {
        String vo = manageService.pass(message);
        return ResponseEntity.ok(vo);
    }

    //动态拒绝
    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody String[] message){
        String vo = manageService.reject(message);
        return ResponseEntity.ok(vo);
    }

    /**
     * 用户冻结操作
     * userId   用户id
     * freezingTime 冻结时间，1为冻结3天，2为冻结7天，3为永久冻结
     * freezingRange 冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
     * reasonsForFreezing 冻结原因
     * frozenRemarks 冻结备注
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freezeUser(@RequestBody Map map) {
        Integer userIdTemp = (Integer) map.get("userId");
        Long userId = Long.valueOf(userIdTemp);
        Integer freezingTime = Integer.valueOf((String) map.get("freezingTime"));
        Integer freezingRange = Integer.valueOf((String) map.get("freezingRange"));
        String reasonsForFreezing = (String) map.get("reasonsForFreezing");
        String frozenRemarks = (String) map.get("frozenRemarks");
        String message = manageService.freezeUser(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks);
        return ResponseEntity.ok(message);
    }

    //用户解冻操作
    //  manage/users/unfreeze
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreezeUser(@RequestBody Map map) {
        Integer userIdTemp = (Integer) map.get("userId");
        Long userId = Long.valueOf(userIdTemp);
        //此处：前端传递的解冻原因在frozenRemarks参数中，reasonsForThawing为空
        String reasonsForThawing = (String) map.get("frozenRemarks");
        String message = manageService.unfreezeUser(userId, reasonsForThawing);
        return ResponseEntity.ok(message);
    }
}
